<template>
    <van-form @submit="onSubmit">
        <van-field
            v-model="editUser.currentValue"
            :name="editUser.editKey"
            :label="editUser.editName"
            :placeholder="'请输入${editUser.editName}'"
        />
      <div style="margin: 16px;">
        <van-button round block type="primary" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </template>
  
  <script setup lang="ts">
  import {useRoute, useRouter} from "vue-router";
  import {ref} from "vue";
  import { showFailToast, showSuccessToast } from "vant/lib/vant.es";
  import myAxios from "../plugins/myAxios.ts";
  import { getCurrentUser } from "../services/user";

  const route = useRoute();
  const router = useRouter();
  const editUser = ref({
    editKey: route.query.editKey,
    editName: route.query.editName,
    currentValue: route.query.currentValue,
  })
  const onSubmit = async() => {
    const currentUser = await getCurrentUser();
    if(!currentUser){
      showFailToast("用户未登录");
      return;
    }
    const res = await myAxios.post('user/update', {
      'id': currentUser.id,
      [editUser.value.editKey]: editUser.value.currentValue
    })
    //todo 把editKey currentValue editName提交到后台
    console.log('更新请求',res);
    console.log("res.code: " + res.code + "\n res.data: " + res.data)
    if(res.code === 0 && res.data > 0){
      showSuccessToast('修改成功')
      router.back()
    } else {
      showFailToast('修改失败')
    }
  }
  
  </script>